﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MassPokerTool.Infrastructure.Threading;
using Microsoft.Practices.ServiceLocation;

namespace MassPokerTool.Infrastructure.Hooking
{
    public class MessageBus
    {
        private static readonly object SyncRoot;
        private static readonly IDispatcherService Dispatcher;

        static MessageBus()
        {
            SyncRoot = new object();
            Messages = new ObservableCollection<Message>();
            Dispatcher = ServiceLocator.Current.GetInstance<IDispatcherService>();
        }

        public static void AddMessage(string category, string message)
        {
            lock (SyncRoot)
            {
                Dispatcher.Invoke(() => Messages.Add(new Message()
                                                          {
                                                              Timestamp = DateTime.Now,
                                                              Category = category,
                                                              Text = message
                                                          }));
            }
        }

        public static void AddMessages(string category, IEnumerable<string> messages)
        {
            lock (SyncRoot)
            {
                Dispatcher.Invoke(() =>
                                      {
                                          foreach (var message in messages)
                                          {
                                              Messages.Add(new Message()
                                                               {
                                                                   Timestamp = DateTime.Now,
                                                                   Category = category,
                                                                   Text = message
                                                               });
                                          }
                                      });
            }
        }

        public static ObservableCollection<Message> Messages { get; private set; }
    }
}
